package com.vinoxm.common.auth.server.resource.dao;

import com.vinoxm.common.auth.server.resource.model.AuthResource;
import com.vinoxm.common.auth.server.resource.model.AuthRole;
import com.vinoxm.common.auth.server.resource.model.AuthUri;
import com.vinoxm.common.auth.server.resource.model.AuthUser;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface AuthDao {

    @Select("SELECT resource_id, role_ids FROM auth_resource_role WHERE resource_id=#{resourceId}")
    AuthResource selResourceById(String resourceId);

    @Select("SELECT " +
            "r.role_id,r.role_name,r.role_description " +
            "FROM auth_user_role ur " +
            "LEFT JOIN auth_role r ON ur.role_id=r.role_id " +
            "WHERE ur.user_id=#{userId}")
    List<AuthRole> selRoleByUserId(long userId);

    @Select("SELECT resource_id,uri,role_ids FROM auth_uri WHERE resource_id=#{resourceId}")
    List<AuthUri> selUriByResourceId(String resourceId);

    @Select("SELECT user_id,username,password,create_time,is_del FROM auth_user WHERE user_id=#{userId}")
    @Results( value = {
            @Result(column = "user_id", property = "userId", id = true),
            @Result(column = "user_id", property = "roles", many = @Many(select = "com.vinoxm.common.auth.server.resource.dao.AuthDao.selRoleByUserId"))
    })
    AuthUser selUserById(long usrId);
}
